package com.yxqz.manage_cms.controller;

import com.xuecheng.api.cms.CmsPageTemplateApi;
import com.xuecheng.framework.web.BaseController;
import com.yxqz.manage_cms.service.CmsPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author 余小小
 * 页面静态化
 */
@Controller
public class CmsPagePreviewController extends BaseController  implements CmsPageTemplateApi {

    //    注入静态页面服务
    @Autowired
    CmsPageService cmsPageService;

    /**
     * 页面预览
     * 通过页面id查询在grifs里的静态页面
     */
    @RequestMapping(value = "/cms/preview/{pageId}", method = RequestMethod.GET)
    public void preview(@PathVariable("pageId") String pageId) {
//        执行静态化
        String pageHtml = cmsPageService.getPageHtml(pageId);
//        通过response对象输出内容到页面，和servlet中使用方法一样  response 是BaseController中的
        try {
            response.setContentType("text/html");
//            response.setHeader("Content‐type","text/html;charset=utf‐8");
            response.getOutputStream().write(pageHtml.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
